package cn.com.callback

import groovy.json.*;
import groovy.swing.impl.DefaultAction
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.crypto.Cipher
import javax.crypto.SecretKey
import javax.crypto.spec.SecretKeySpec
import javax.servlet.RequestDispatcher;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.ParseException
import java.text.SimpleDateFormat;
import java.time.Instant

import org.apache.commons.codec.binary.Base64
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import cn.com.kh.hst.ThreeDesUtil
import cn.com.kh.KHUtil;
import cn.com.kh.getProperty;


/**
 *
 * @Title: aipHSTCounselor
 * @Description:华盛证券 唤回 --内部接口
 * @author: xierm
 * @date 2022-19-30 16:00:00
 *
 */

public class aipHSTLabelCustomers2 implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(aipHSTLabelCustomers2.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        log.debug("处理开始时间:" + startTime);

        long endTime = System.currentTimeMillis();

        String reJson = null;
        try {
            String tab_activity_id="";
			String frequency="";
			String batchid="";
			String customer_guid="";
			String objective_guid="";
			String hst_no="";
			String rec_id="";
			
			List date_formatlist = eliteBean.chaxun_sql("select date_format(curdate(),'%w') AS week,DATE_FORMAT(NOW(), '%d') AS week2 from dual", "dataSource");
            def date_format = date_formatlist.get(0).get("week");
            def date_format2 = date_formatlist.get(0).get("week2");
			log.debug("date_format:" + date_format);
			log.debug("date_format2:" + date_format2);
			
           	List custCountList = eliteBean.chaxun_sql("select tab_activity_id,frequency from hst_Activity where tab_activity_rule='唤回活动规则'", "dataSource");//查询批次
			log.debug("custCountList:" + custCountList.size);
                    if (custCountList.size() > 0) {
	                     int arrInt = custCountList.size();
	                     for (int i = 0; i < arrInt; i++) {
							tab_activity_id = custCountList.get(i).get("tab_activity_id");
							frequency = custCountList.get(i).get("frequency");
							log.debug("frequency:" + frequency);
							
							if(frequency=="1"){//批次每日
							    List batchidlist = eliteBean.chaxun_sql("select batchid from hst_activity_batch where tab_activity_id='"+tab_activity_id+"'", "dataSource");
                                batchid = batchidlist.get(0).get("batchid");
								
								List batchidlist2 = eliteBean.chaxun_sql("select hst_no,customer_guid,objective_guid,rec_id from hst_activity_batch_list  where result_code='1' and result_desc!='已推送' and batchid='"+batchid+"'", "dataSource");
								log.debug("batchidlist2:" + batchidlist2.size());
								if(batchidlist2.size() > 0){
								int arrInt2 = batchidlist2.size();	
									for (int t = 0; t < arrInt2; t++) {
									customer_guid = batchidlist2.get(t).get("customer_guid");
									log.debug("customer_guid:" + customer_guid);
								    objective_guid = batchidlist2.get(t).get("objective_guid");
									log.debug("objective_guid:" + objective_guid);
								    hst_no = batchidlist2.get(t).get("hst_no");
									log.debug("hst_no:" + hst_no);
									rec_id = batchidlist2.get(t).get("rec_id");
									log.debug("rec_id:" + rec_id);
								
								    String[] custTaskDealSQL = new String[2];
								
							        // 插入elitetask_activity表
						          	custTaskDealSQL[0] = "INSERT INTO elitetask_activity" +
									"  (hst_no, " +
									"  batchid, " +
									"  tab_activity_id, " +
									"  customer_guid, " +
									"  objective_guid, " +
									"  Createdby, " +
									"  Createddate) " +
									"  VALUES" +
									"  ('" + hst_no + "' , " +
									"  '" + batchid + "' ," +
									"  '" + tab_activity_id + "' , " +
									"  '" + customer_guid + "' ," +
									"  '" + objective_guid + "' , " +
									"  'SELITE' , " +
									"now())"	
									
									// 修改hst_activity_batch_list状态
						          	custTaskDealSQL[1] = "update hst_activity_batch_list set result_desc='已推送' where rec_id='"+rec_id+"'";
									
									int[] batchSQLResult = eliteBean.updateBatch(custTaskDealSQL, "dataSource");
									log.debug("SQL返回结果："+batchSQLResult.toString());
									}
								}
							}
							
							if(frequency=="2"&&date_format=="1"){//批次每周
							    List batchidlist = eliteBean.chaxun_sql("select batchid from hst_activity_batch where tab_activity_id='"+tab_activity_id+"'", "dataSource");
                                batchid = batchidlist.get(0).get("batchid");
								
								List batchidlist2 = eliteBean.chaxun_sql("select hst_no,customer_guid,objective_guid,rec_id from hst_activity_batch_list  where result_code='1' and result_desc!='已推送' and batchid='"+batchid+"'", "dataSource");
								log.debug("batchidlist2:" + batchidlist2.size());
								if(batchidlist2.size() > 0){
								int arrInt2 = batchidlist2.size();	
									for (int t = 0; t < arrInt2; t++) {
									customer_guid = batchidlist2.get(t).get("customer_guid");
									log.debug("customer_guid:" + customer_guid);
								    objective_guid = batchidlist2.get(t).get("objective_guid");
									log.debug("objective_guid:" + objective_guid);
								    hst_no = batchidlist2.get(t).get("hst_no");
									log.debug("hst_no:" + hst_no);
									rec_id = batchidlist2.get(t).get("rec_id");
									log.debug("rec_id:" + rec_id);
								
								    String[] custTaskDealSQL = new String[2];
								
							        // 插入elitetask_activity表
						          	custTaskDealSQL[0] = "INSERT INTO elitetask_activity" +
									"  (hst_no, " +
									"  batchid, " +
									"  tab_activity_id, " +
									"  customer_guid, " +
									"  objective_guid, " +
									"  Createdby, " +
									"  Createddate) " +
									"  VALUES" +
									"  ('" + hst_no + "' , " +
									"  '" + batchid + "' ," +
									"  '" + tab_activity_id + "' , " +
									"  '" + customer_guid + "' ," +
									"  '" + objective_guid + "' , " +
									"  'SELITE' , " +
									"now())"	
									
									// 修改hst_activity_batch_list状态
						          	custTaskDealSQL[1] = "update hst_activity_batch_list set result_desc='已推送' where rec_id='"+rec_id+"'";
									
									int[] batchSQLResult = eliteBean.updateBatch(custTaskDealSQL, "dataSource");
									log.debug("SQL返回结果："+batchSQLResult.toString());
									}
								}
							}
							
							if(frequency=="3"&&date_format2=="1"){//批次每月
							    List batchidlist = eliteBean.chaxun_sql("select batchid from hst_activity_batch where tab_activity_id='"+tab_activity_id+"'", "dataSource");
                                batchid = batchidlist.get(0).get("batchid");
								
								List batchidlist2 = eliteBean.chaxun_sql("select hst_no,customer_guid,objective_guid,rec_id from hst_activity_batch_list  where result_code='1' and result_desc!='已推送' and batchid='"+batchid+"'", "dataSource");
								log.debug("batchidlist2:" + batchidlist2.size());
								if(batchidlist2.size() > 0){
								int arrInt2 = batchidlist2.size();	
									for (int t = 0; t < arrInt2; t++) {
									customer_guid = batchidlist2.get(t).get("customer_guid");
									log.debug("customer_guid:" + customer_guid);
								    objective_guid = batchidlist2.get(t).get("objective_guid");
									log.debug("objective_guid:" + objective_guid);
								    hst_no = batchidlist2.get(t).get("hst_no");
									log.debug("hst_no:" + hst_no);
									rec_id = batchidlist2.get(t).get("rec_id");
									log.debug("rec_id:" + rec_id);
								
								    String[] custTaskDealSQL = new String[2];
								
							        // 插入elitetask_activity表
						          	custTaskDealSQL[0] = "INSERT INTO elitetask_activity" +
									"  (hst_no, " +
									"  batchid, " +
									"  tab_activity_id, " +
									"  customer_guid, " +
									"  objective_guid, " +
									"  Createdby, " +
									"  Createddate) " +
									"  VALUES" +
									"  ('" + hst_no + "' , " +
									"  '" + batchid + "' ," +
									"  '" + tab_activity_id + "' , " +
									"  '" + customer_guid + "' ," +
									"  '" + objective_guid + "' , " +
									"  'SELITE' , " +
									"now())"	
									
									// 修改hst_activity_batch_list状态
						          	custTaskDealSQL[1] = "update hst_activity_batch_list set result_desc='已推送' where rec_id='"+rec_id+"'";
									
									int[] batchSQLResult = eliteBean.updateBatch(custTaskDealSQL, "dataSource");
									log.debug("SQL返回结果："+batchSQLResult.toString());
									}
								}
							}
						 }
						}
                    reJson = "{\"result\":\"11\",\"message\":\"处理成功\"}";
        } catch (Exception e) {
            log.error("处理异常" + e.getMessage());
            // 失败返回
            reJson = "{\"result\":\"-1\",\"message\":\"处理失败\"}";
        }
        return reJson;
    }




    // 解析Json值
    public static String GetJV(JSONObject inJson, String getKey) {
        String Rev_Value = null;
        try {
            Rev_Value = inJson.getString(getKey);
        } catch (Exception e) {
            try {
                Rev_Value = String.valueOf(inJson.getInt(getKey));
            } catch (Exception e1) {
                try {
                    Rev_Value = String.valueOf(inJson.getJSONArray(getKey));
                } catch (Exception e2) {
                    try {
                        Rev_Value = String.valueOf(inJson.getJSONObject(getKey));
                    } catch (Exception e3) {
                        Rev_Value = "";
                    }
                }
            }
        } finally {
            if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
                Rev_Value = "";
            } else {
            }
        }
        log.debug("解析-k=" + getKey + " v=" + Rev_Value);
        return Rev_Value;
    }


    // 非空校验isNull
    public static boolean isNull(String value) {

        boolean reFlag = true;
        if (value == "" || value.length() <= 0) {
            reFlag = true;
        } else {
            reFlag = false;
        }
        log.debug(reFlag);
        return reFlag;
    }

    // 非空校验isNullMSG
    public static String isNullMSG(String key, String value) {

        String reResult = "";
        if (value == "" || value.length() <= 0) {
            reResult = key + "必填字段;";
        }
        return reResult;
    }

}
